<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>用户管理插入子页面</title>
  <script src="../../js/jquery-3.4.1.min.js"></script>
  <script src="../../layui/layui.js"></script>
  <link rel="stylesheet" href="../../layui/css/layui.css">
</head>

<body>
  <form class="layui-form" method="post" style="margin-top: 20px">
    <div class="layui-form-item" style="padding-right: 50px; width: 300px; margin: 0 auto; margin-top: 15px">
      <label class="layui-form-label">登录名</label>
      <div class="layui-input-block">
        <input id="auth-username" type="text" name="title" required lay-verify="required" placeholder="请输入登录名"
          autocomplete="off" class="layui-input">
      </div>
    </div>
    <!--<div class="layui-form-item" style="padding-right: 50px; width: 300px; margin: 0 auto; margin-top: 15px ">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input id="auth-name" type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>-->
    <div class="layui-form-item" style="padding-right: 50px; width: 300px; margin: 0 auto; margin-top: 15px ">
      <label class="layui-form-label">密码</label>
      <div class="layui-input-block">
        <input id="auth-password" type="password" name="title" required lay-verify="required" placeholder="请输入密码"
          autocomplete="off" class="layui-input" maxlength="20">
      </div>
    </div>
    <div class="layui-form-item" style="padding-right: 50px; width: 300px; margin: 0 auto; margin-top: 15px ">
      <label class="layui-form-label">管理员</label>
      <div class="layui-input-block">
        <input id="auth-isAdmin" type="checkbox" lay-filter="insert_switch" lay-skin="switch" name="close"
          lay-text="是|否" checked>
      </div>
    </div>

    <div class="layui-form-item" style="padding-right: 40px; width: 300px; margin: 0 auto; margin-top: 40px">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>

  <script>
    //Demo
    layui.use('form', function () {
      var form = layui.form;
      var callbackData;
      var nowDate = new Date();
      //监听提交
      form.on('submit(formDemo)', function (data) {

        var loginName = $("#auth-username").val();
        if (!loginName.match(/^[a-zA-Z]\w{4,10}$/)) {
          parent.layer.msg('用户名长度必须在5-10位，只能是英文大小写，数字，首字符不能为数字', {
                icon: 2
          });
          return false;
        }
        var password = $("#auth-password").val();
        if(!password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8}/)){
          parent.layer.msg('密码最少8个字符，至少1个大写字母，1个小写字母，1个数字和1个特殊字符[$@$!%*?&]', {
            icon: 2
          });
          return false;
        }

        $.ajax({
          url: '/fjgjtime/auths',
          method: 'post',
          data: {
            username: loginName,
            password: password,
            isAdmin: $("#auth-isAdmin").prop("checked"),
            createdTime: nowDate
          },
          success: function (res) {
            if (res.code == 200) {
              callbackData = {
                username: loginName,
                // password: $("#auth-password").val(),
                isAdmin: $("#auth-isAdmin").prop("checked"),
                createdTime: nowDate
              };
              parent.layerCallback(callbackData);
              parent.layer.msg('添加用户成功!', {
                icon: 1
              });
              var index = parent.layer.getFrameIndex(window.name);
              parent.layer.close(index);
            } else {
              parent.layer.msg('添加用户失败 '+res.msg, {
                icon: 2
              });
            }
          }
        });
        return false;
      });
    });
  </script>
</body>

</html>